<?php 
namespace app\index\controller;

use think\Controller;
use think\Request;
use app\common\controller\Curls;
use app\common\model\CompanyStaff;
use think\Exception;
use app\common\model\CompanyStaffDepart;
use app\common\controller\Wechat;

class Wxpublic extends Controller
{
	private $appId = 'wxfe8a528cd8fc968d';
	private $appSecret = '6003847226b40fa38f032d143d64ce35';
	
	/**
	 * 微信登录
	 * @param Request $request
	 * @throws Exception
	 */
	public function wxLogin(Request $request)
	{
		$code = $request->param('code');
		$url_param = $request->param('state');
		if($code){
			$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appId}&secret={$this->appSecret}&code={$code}&grant_type=authorization_code";
			$tocken = json_decode(file_get_contents($url),true);
			$access_token = $tocken['access_token'];
			$openid = $tocken['openid'];
			$get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
			$user_info = Curls::curl($get_user_info_url);
			//获取用户信息
			$user_info = json_decode($user_info,true);
			//判断该用户是否绑定员工
			$staff_id = CompanyStaff::where(['openid' => $user_info['openid']])->value('id');
			session('openid',$user_info['openid']);
			if(!$staff_id){
				
				session('nick_name',$user_info['nickname']);
				session('headimg',$user_info['headimgurl']);
				//去绑定员工
				$this->redirect('https://'.$_SERVER['HTTP_HOST'].'/#/pages/checkwork/bindsatff');
			}
			CompanyStaff::get($staff_id)->save(['headimg' => $user_info['headimgurl'],'nick_name' => $user_info['nickname']]);
			session('staff_id',$staff_id);
			$url = '';
			switch ($url_param) {
				case 1:
					// 消息通知
					$url = 'https://'.$_SERVER['HTTP_HOST'].'/#/pages/index/index';
					break;
				case 2:
					//考勤
					$url = 'https://'.$_SERVER['HTTP_HOST'].'/#/pages/checkwork/index';
					break;
				case 3:
					//个人中心
					$url = 'https://'.$_SERVER['HTTP_HOST'].'/#/pages/user/index';
					break;
			}
			$this->redirect($url);
			//echo '登录成功';
		}
	}
	
	/**
	 * 绑定员工
	 * @param Request $request
	 * @throws Exception
	 */
	public function bindStaff(Request $request)
	{
		$no = $request->param('no');
		try {
			$staff_id = CompanyStaffDepart::where(['no' => $no])->value('staff_id');
			if(!$staff_id)throw new Exception('员工工号错误');
			$staff = CompanyStaff::get($staff_id);
			if($staff->openid)throw new Exception('该员工已绑定');
			//绑定员工
			$staff->save(['openid' => session('openid'),'nick_name' => session('nick_name'),'headimg' =>  session('headimg')]);
		} catch (Exception $e) {
			return $this->error($e->getMessage());
		}
		//给用户公众号推送消息
		session('staff_id',$staff_id);
		$staff_info = CompanyStaff::get(session('staff_id'));
		$tmep_data = [
				"first" => [
						"value" => "恭喜{$staff_info->name}，成为安徽微联广告传媒有限公司的一员",
						"color" => "#000000"
				],
				"keyword1" => [
						"value" => $staff_info->depart_name.'-'.$staff_info->post_name,
						"color" => "#173177"
				],
				"keyword2" => [
						"value" => "安徽微联广告传媒有限公司",
						"color" => "#333333"
				],
				"keyword3" => [
						"value" => date('Y-m-d H:i:s'),
						"color" => "#333333"
				],
				"keyword4" => [
						"value" => "安徽省合肥市瑶海区中环国际大厦A1座1805",
						"color" => "#333333"
				],
				"remark" => [
						"value" => "记得以后上班按时打卡哦~",
						"color" => "#333333"
				]
		];
		Wechat::modelMsg($staff_info->openid, 'JlEG3mrMXbb1Sonrrsj2HovPPeAA0Pn0PFhpiT_XD0I', $tmep_data);
		return $this->success('绑定成功');
	}
	
	/**
	 * 获取ACCESS_TOKEN
	 * @return mixed
	 */
	private function getToken()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
		$res = json_decode(Curls::curl($url),true);
		return $res['access_token'];
	}
}